package com.nl.math;

public class MatrixCirclePrinter {

	public static void printMatrix(int[][] matrix,int rows,int columns){
		
		if(matrix==null ||rows<1||columns<1){
			return ;
		}
		int start =0;
		while(rows>start*2&&columns>start*2){
			printMatrixInCircle(matrix,rows,columns,start);
			start++;
		}
	}

	private static void printMatrixInCircle(int[][] matrix, int rows, int columns,
			int start) {
		int endX=columns-1-start;
		int endY=rows-1-start;

		//left -> right
		for(int i=start;i<=endX;++i){
			int number = matrix[start][i];
			System.out.print(number+" ");
		}

		//up -> down
		if(start <endY){
			for(int i=start+1;i<=endY;i++){
				int number =matrix[i][endY];
				System.out.print(number+" ");
			}
		}
		
		//right -> left
		if(start<endX&&start<endY){
			for(int i=endX-1;i>=start;--i){
				int number=matrix[endY][i];
				System.out.print(number+" ");
			}
		}

		//down -> up
		if(start<endX&&start<endY-1){
			for(int i=endY-1;i>=start+1;--i){
				int number = matrix[i][start];
				System.out.print(number+" ");
			}
		}

	}
}
